package com.guofen.controller.hsmpy;

import com.guofen.pojo.HsmSmallWorm;
import com.guofen.pojo.SjqFlocculationCraftScene;
import com.guofen.service.hsmpy.HatchingFeedService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/hsmpy/hatchingFeed")
@Controller
@Api(tags = "HatchingFeedController", description = "1-1 孵化进料记录表")
public class HatchingFeedController {

    @Autowired
    private HatchingFeedService hatchingFeedService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name = "hatch_start_time", value = "开始孵化日期", required = true) @RequestParam String hatch_start_time,
                      @ApiParam(name = "batch", value = "批次", required = true) @RequestParam String batch,
                      @ApiParam(name = "worm_group", value = "组别", required = true) @RequestParam String worm_group,
                      @ApiParam(name = "hatch_box_number", value = "孵化盒号", required = true) @RequestParam String hatch_box_number,
//                      @ApiParam(name = "hatch_box_weight", value = "孵化盒重", required = true) @RequestParam String hatch_box_weight,
                      @ApiParam(name = "ferment_material_type", value = "发酵料种类", required = true) @RequestParam String ferment_material_type,
                      @ApiParam(name = "ferment_material_weight", value = "发酵料重量", required = true) @RequestParam String ferment_material_weight,
                      @ApiParam(name = "fuliao_name", value = "辅料名称", required = true) @RequestParam String fuliao_name,
                      @ApiParam(name = "fuliao_weight", value = "培育料辅料重量", required = true) @RequestParam String fuliao_weight,
                      @ApiParam(name = "chongluanfuliao_weight", value = "虫卵辅料重量", required = true) @RequestParam String chongluanfuliao_weight,
                      @ApiParam(name = "water_weight", value = "水", required = true) @RequestParam String water_weight,
                      @ApiParam(name = "egg_weight", value = "虫卵重量", required = true) @RequestParam String egg_weight,
                      @ApiParam(name = "clfpslbw_weight", value = "虫卵+麸皮+塑料杯+网重量", required = true) @RequestParam String clfpslbw_weight,
                      @ApiParam(name = "remark", value = "虫卵来源", required = true) @RequestParam String remark){
//        Double hatch_box_w = Double.parseDouble(hatch_box_weight);
        Double ferment_material_w = Double.parseDouble(ferment_material_weight);
        Double fuliao_w = Double.parseDouble(fuliao_weight);
        Double chongluanfuliao_w = Double.parseDouble(chongluanfuliao_weight);
        Double water_w = Double.parseDouble(water_weight);
        Double egg_w = Double.parseDouble(egg_weight);
        Double clfpslbw_w = Double.parseDouble(clfpslbw_weight);
        if (hatchingFeedService.add(hatch_start_time, batch, worm_group, hatch_box_number, ferment_material_type,
                ferment_material_w, fuliao_name, fuliao_w, chongluanfuliao_w, water_w, egg_w, clfpslbw_w, remark))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("返回当天最新一条数据")
    @RequestMapping(value = "/getlatest", method = RequestMethod.GET)
    @ResponseBody
    public Result getlatest(){
        return ResultGenerator.genSuccessResult(hatchingFeedService.getlatest());
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(hatchingFeedService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="hatchStartTime",value="开始孵化日期",required=true) @RequestParam String hatchStartTime,
                         @ApiParam(name="batch",value="批次",required=true) @RequestParam String batch,
                         @ApiParam(name="wormGroup",value="组别",required=true) @RequestParam String wormGroup,
                         @ApiParam(name="hatchBoxNumber",value="孵化盒号",required=true) @RequestParam String hatchBoxNumber,
                         @ApiParam(name="fermentMaterialWeight",value="发酵料（kg）",required=true) @RequestParam Double fermentMaterialWeight,
                         @ApiParam(name="fuliaoWeight",value="辅料重量（kg）",required=true) @RequestParam Double fuliaoWeight,
                         @ApiParam(name="waterWeight",value="水（kg）",required=true) @RequestParam Double waterWeight,
                         @ApiParam(name="clfpslbwWeight",value="孵化网重（g）",required=true) @RequestParam Double clfpslbwWeight,
                         @ApiParam(name="eggWeight",value="虫卵重量（g）",required=true) @RequestParam Double eggWeight,
                         @ApiParam(name="chongluanfuliaoWeight",value="孵化辅料重量(g)",required=true) @RequestParam Double chongluanfuliaoWeight) {
        HsmSmallWorm record = new HsmSmallWorm();
        record.setId(id);
        record.setHatchStartTime(hatchStartTime);
        record.setBatch(batch);
        record.setWormGroup(wormGroup);
        record.setHatchBoxNumber(hatchBoxNumber);
        record.setFermentMaterialWeight(fermentMaterialWeight);
        record.setFuliaoWeight(fuliaoWeight);
        record.setWaterWeight(waterWeight);
        record.setClfpslbwWeight(clfpslbwWeight);
        record.setEggWeight(eggWeight);
        record.setChongluanfuliaoWeight(chongluanfuliaoWeight);
        return ResultGenerator.genSuccessResult(hatchingFeedService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(hatchingFeedService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(hatchingFeedService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
